﻿using System;
using System.Collections;
using System.Linq;
using System.Windows;
using CustomerAssistant.Core;
using CustomerAssistant.Properties;

namespace CustomerAssistant
{
    public class OrderMonitor
    {
        public static System.Timers.Timer OrderRefreshTimer;

        public static IEnumerable ItemsSource = null;

        static Window CurrentWindow = null;
        /// <summary>
        /// 上一次从服务器请求数据的hash,如果有变动则提醒用户
        /// </summary>
        static string LastHash = string.Empty;

        public static void Run()
        {
            Notice();
            //单独的线程中,职责是定时从服务器上下载数据
            OrderRefreshTimer = new System.Timers.Timer();
            OrderRefreshTimer.Elapsed += new System.Timers.ElapsedEventHandler((s, e) =>
            {
                if (GetData())
                {
                    TrayNotifyUtil.FlashTrayIcon();
                }
            });
            OrderRefreshTimer.Interval = 10000;
            OrderRefreshTimer.Start();
        }

        private static bool GetData()
        {
            var xml = Util.HttpRequest(Settings.Default.serviceUrl
                , string.Format("handle=orderstat&uid={0}&pwd={1}&cid={2}&token={3}"
                , Login.OperatorLoginData.OperatorID, Login.OperatorLoginData.Password, Login.OperatorLoginData.ClientID
                , Util.CreateMd5(DateTime.Now.AddHours(1).ToString("yyMMddhh") + Login.OperatorLoginData.OperatorID)));
            if (string.IsNullOrEmpty(xml))
            {
                LogHelper.LogInfo(xml);
                return false;
            }
            var hash = Util.CreateMd5(xml);
            if (string.Compare(LastHash, hash, true) != 0)
            {
                //信息有变更
                LastHash = hash;
                ItemsSource = from item in System.Xml.Linq.XDocument.Parse(xml).Root.Elements("OrderNoticeItem") select item;
                return true;
            }
            return false;
        }

        public static void Notice()
        {
            CurrentWindow.Dispatcher.BeginInvoke(new Action(() =>
            {
                if (ItemsSource == null)
                    GetData();//登录后打开
                CreateNotice();
            }));
        }

        public static void CreateNotice()
        {
            /*
             * 本来在这想写单例,但是有以下原因放弃:
             * 1.单例情况下,把窗体隐藏后再打开,却打不开
             */
            if (CurrentWindow != null)
                CurrentWindow.Close();
            CurrentWindow = new Window();
            var infotip = new InfoTip();
            var ordertip = new OrderTip();
            CurrentWindow.Topmost = true;
            CurrentWindow.ShowInTaskbar = false;
            CurrentWindow.Title = "待处理事项";
            infotip.ShowMinimizedBtn = false;
            infotip.grid1.Children.Add(ordertip);
            infotip.Height = 300;
            ordertip.listbox1.ItemsSource = ItemsSource;
            if (ItemsSource != null)
                TrayNotifyUtil.ShowInfoTip(CurrentWindow, infotip, "RB", false, true);
        }
    }
}